<?php

/**
 * Class simpleModel
 * 单例模式
 */
class simpleModel{

    private static $db = null;
    private static $pdo;

    //私有化构造方法
    private function __construct(){
        $opption=array(
            PDO::ATTR_PERSISTENT=>true,			//开启持久连接
            PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION	//报错级别可选参数
        );
        try{
            self::$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8','root','root',$opption); //连接数据库
        }catch (PDOException $e){
            echo $e->getMessage();
        }
    }

    //实例化对象
    public static function getInstance(){
        if(is_null(self::$db))
            self::$db = new self();

        return self::$db;
    }

    //查询数据
    public function query(){
        $sql = "select * from t1";
        return self::$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
    }

    //增加数据
    public function addDate(){
        $sql = "insert into t1 (name) values('增加数据')";
        return self::$pdo->exec($sql);
    }

    //私有化clone方法防止被clone
    private function __clone(){}

    //私有化析构方法
    //private function __destruct(){}

}

$obj = simpleModel::getInstance();

//print_r($obj->query());
$obj->addDate();

